package bar.dark.lee.common;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import bar.dark.lee.R;

public class CheckBoxDialogActivity extends ListActivity
{
	public static final String CHECKSTRING = "check_string";
	public static final String CHECKDATA = "check_data";
	public static final String CHECKSELECT = "check_select";

	private ArrayList<String> list_;
	ArrayList<String> select_;
	private int [] checkBox;
	
	private Button checkBoxButton;
	private Button checkBoxButtonAll;
	
	private CheckBoxDialogAdapter adapter;
	private boolean checkAllFlag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.checkbox_activity);
		
		checkBoxButton = (Button) findViewById(R.id.checkBoxButton);
		checkBoxButtonAll = (Button) findViewById(R.id.checkBoxButtonAll);
		
		Intent intent = getIntent();
		list_ = intent.getStringArrayListExtra(CHECKDATA);
		select_ = intent.getStringArrayListExtra(CHECKSELECT);
		
		if (select_ == null)
		{
			return;
		}
		
		if (list_ == null)
		{
			return;
		}
		
		checkBox = new int[list_.size()];
		
		for (int i = 0; i < checkBox.length; i++)
		{
			checkBox[i] = 0;
		}

		if (select_.size() > 0)
		{
			for (int i=0; i<select_.size(); i++)
			{
				checkBox[Integer.parseInt(select_.get(i))] = 1;
			}
			
			if (checkBox.length == select_.size())
			{
				checkAllFlag = true;
				checkBoxButtonAll.setText("모두 해제");
			}
		}
		
		getListView().setFooterDividersEnabled(true);
		
		adapter = new CheckBoxDialogAdapter(this, R.layout.checkbox_list_item_dialog, list_);
		adapter.setNotifyOnChange(true);
		setListAdapter(adapter);
		
		checkBoxButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(CHECKSELECT, checkBox);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
		
		checkBoxButtonAll.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if ( ! checkAllFlag)
				{
					for (int i = 0; i < checkBox.length; i++)
					{
						checkBox[i] = 1;
					}
					
					adapter.notifyDataSetChanged();
					
					checkBoxButtonAll.setText("모두 해제");
					
					checkAllFlag = true;
				}
				else
				{
					for (int i = 0; i < checkBox.length; i++)
					{
						checkBox[i] = 0;
					}
					
					adapter.notifyDataSetChanged();
					
					checkBoxButtonAll.setText("모두 선택");
					
					checkAllFlag = false;
				}
			}
		});
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id)
	{
		super.onListItemClick(l, v, position, id);
		
//		for (int i = 0; i < l.getChildCount(); i++)
//		{
//			ViewGroup vg = (ViewGroup) l.getChildAt(i);
//			ViewGroup vgc = (ViewGroup) vg.getChildAt(0);
//			CheckBox c = (CheckBox) vgc.getChildAt(1);
//			c.setChecked(false);
//		}
		
		CheckBox cb = (CheckBox) v.findViewById(R.id.checkBox);
		
		if (cb.isChecked())
		{
			cb.setChecked(false);
			checkBox[position] = 0;
		}
		else
		{
			cb.setChecked(true);
			checkBox[position] = 1;
		}
		
//		Intent intent = new Intent();
//		intent.putExtra(CHECKSTRING, list_.get(position));
//		setResult(Activity.RESULT_OK, intent);
//		finish();
	}
	
	public class CheckBoxDialogAdapter extends ArrayAdapter<String>
	{
		ArrayList<String> list_;
		
		public CheckBoxDialogAdapter(Context context, int textViewResourceId,
				ArrayList<String> list)
		{
			super(context, textViewResourceId, list);
			this.list_ = list;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v  = li.inflate(R.layout.checkbox_list_item_dialog, null);
			}
			
			String data = list_.get(position);
			
			BDTextView tv = (BDTextView) v.findViewById(R.id.checkText);
			CheckBox rb = (CheckBox) v.findViewById(R.id.checkBox);
			
			if (checkBox[position] == 1)
			{
				rb.setChecked(true);
			}
			else
			{
				rb.setChecked(false);
			}
			
			tv.setText(data);
			
			return v;
		}
	}
}
